<template>
  <div>
    <div>
      <el-button size="small" @click="Addlaber">保存</el-button>
      <el-button size="small" v-if="Yes" @click="examine">提交审批</el-button>
    </div>

    <div>
      <font size="6" bold style="position:relative;left:700px">劳务合同</font>
    </div>
    <br />

    <el-form :model="laber" label-width="100px" class="demo-ruleForm">
      <!-- 第一个表格 -->
      <table class="divtable" width="100%">
        <tr>
          <td colspan="8" class="color">基本信息</td>
        </tr>
        <tr>
          <td class="tabletd">日期:</td>
          <td>
            <el-input type="date" v-model="laber.create_date" value-format="yyyy-MM-dd" clearable></el-input>
          </td>
          <td class="tabletd">合同编号:</td>
          <td>
            <el-input v-model="laber.id" clearable></el-input>
          </td>
          <td class="tabletd">合同名称:</td>
          <td colspan="3">
            <el-input v-model="laber.contract_name" clearable></el-input>
          </td>
          <td class="tabletd">日期:</td>
          <td>
            <el-input type="date" v-model="laber.create_date" value-format="yyyy-MM-dd"></el-input>
          </td>
          <td class="tabletd">合同编号:</td>
          <td>
            <el-input v-model="laber.id" placeholder="保存后自动生成" disabled></el-input>
          </td>
          <td class="tabletd">合同名称:</td>
          <td colspan="3">
            <el-input v-model="laber.contract_name" clearable></el-input>
          </td>
        </tr>
        <tr>
          <td class="tabletd">所属项目:</td>
          <td colspan="3">
            <el-input v-model="laber.p_id" @focus="pid" clearable></el-input>
          </td>
          <td class="tabletd">合同类型:</td>
          <td>
            <el-select v-model="laber.contract_type" clearable placeholder="请选择">
              <el-option value="1">劳务合同</el-option>
            </el-select>
          </td>
          <td class="tabletd">合同金额:</td>
          <td>
            <el-input
              v-model="laber.contract_amount"
              @blur="money"
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="12"
              clearable
            ></el-input>
          </td>
        </tr>
        <tr>
          <td class="tabletd">金额大写:</td>
          <td>
            <el-input v-model="laber.amount_upper" disabled></el-input>
          </td>
          <td class="tabletd">开始日期:</td>
          <td>
            <el-input type="date" v-model="laber.start_date" value-format="yyyy-MM-dd" clearable></el-input>
          </td>
          <td class="tabletd">结束日期:</td>
          <td>
            <el-input type="date" v-model="laber.end_date" value-format="yyyy-MM-dd" clearable></el-input>
          </td>
          <td class="tabletd">劳务班主:</td>
          <td>
            <el-input v-model="laber.labor_org_id" @focus="labororgid" clearable></el-input>
          </td>
        </tr>
        <tr>
          <td class="tabletd">结算方式:</td>
          <td>
            <el-select v-model="laber.settle_type" clearable placeholder="请选择">
              <el-option value="分段结算">分段结算</el-option>
              <el-option value="竣工后一次性结算">竣工后一次性结算</el-option>
            </el-select>
          </td>
          <td class="tabletd">预付款:</td>
          <td>
            <el-input
              v-model="laber.advance_pay"
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="9"
              clearable
            ></el-input>
          </td>
          <td class="tabletd">保证金:</td>
          <td>
            <el-input
              v-model="laber.deposit_amount"
              oninput="value=value.replace(/[^\d]/g,'')"
              maxlength="9"
              clearable
            ></el-input>
          </td>
          <td class="tabletd">付款方式:</td>
          <td>
            <el-select v-model="laber.payment_type" clearable placeholder="请选择">
              <el-option value="按进度付款">按进度付款</el-option>
              <el-option value="按合同付款">按合同付款</el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td class="tabletd">签订人:</td>
          <td>
            <el-input v-model="laber.contract_signatory" @focus="contractsignatory" clearable></el-input>
          </td>
          <td class="tabletd"></td>
          <td></td>
          <td class="tabletd"></td>
          <td></td>
          <td class="tabletd"></td>
          <td></td>
        </tr>
        <tr>
          <td class="tabletd">付款条件:</td>
          <td colspan="7">
            <el-input v-model="laber.pay_condition" type="textarea" rows="4" clearable></el-input>
          </td>
        </tr>
        <tr>
          <td class="tabletd">主要条款:</td>
          <td colspan="7">
            <el-input v-model="laber.main_term" type="textarea" rows="4" clearable></el-input>
          </td>
        </tr>
        <tr>
          <td class="tabletd">备注:</td>
          <td colspan="7">
            <el-input v-model="laber.comment" type="textarea" rows="4" clearable></el-input>
          </td>
        </tr>
      </table>

      <multi-file-upload
        style="width: 100%;margin: 20px auto"
        title="附件"
        action="http://localhost/uploads"
        :params="{folder: 'tenderPurchasePlan'}"
        :headers="{token: token}"
        :show-button="!checkRoute"
        @uploadSuccess="handleUploadSuccess"
        :parent-file-list="fileList"
      ></multi-file-upload>

      <!-- 多行添加 -->
      <div style="width: 100%;margin: 30px auto">
        <div style="width: 100%;">
          <div style="font-weight: 700;font-size: 20px;display: flex">
            <div>|工程量清单</div>
            <div style="margin-left: 30px">
              <el-button v-if="!checkRoute" size="mini" type="primary" @click="handleAddRow">增行</el-button>
            </div>
          </div>
        </div>
        <el-divider></el-divider>
        <el-table :data="tenderPurchaseDetails">
          <el-table-column prop="index" v-if="false"></el-table-column>
          <el-table-column prop="mateId" v-if="false"></el-table-column>
          <el-table-column label="工作内容" prop="work_content">
            <template slot-scope="scope">
              <el-input v-model="scope.row.work_content"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unit">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unit"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="工程量" prop="work_count">
            <template slot-scope="scope">
              <el-input v-model="scope.row.work_count" type="number"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="unit_price">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unit_price" type="number"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="!checkRoute" type="danger" @click="handleRemoveRow(scope.row)">删行</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>
    <!--所属项目子组件-->
    <el-dialog :visible.sync="dialogVisible" width="50%">
      <project-info-list v-if="dialogVisible" @row="project"></project-info-list>
    </el-dialog>

    <!--劳务班组子组件-->
    <el-dialog :visible.sync="dialogVisible2" width="50%">
      <providerinfo v-if="dialogVisible2" @row="provider"></providerinfo>
    </el-dialog>

    <!-- 员工子组件 -->
    <el-dialog :visible.sync="dialogVisible3" width="50%">
      <emp-dept-post v-if="dialogVisible3" @row="empDeptPost"></emp-dept-post>
    </el-dialog>
  </div>
</template>

<script>
import ProjectInfoList from "../common/projectInfo/ProjectInfoList.vue";
import EmpDeptPost from "../common/providerinfo/EmpDeptPost.vue";
import Providerinfo from "../common/providerinfo/Providerinfo.vue";
import LaoWuHeTong from "@/model/smj/LaoWuHeTong";
const Laowuhetong = new LaoWuHeTong();
import { DateUtil } from "@/model/common/DateUtil";

export default {
  name: "LaoWuHeTong",
  components: { ProjectInfoList, Providerinfo, EmpDeptPost },
  directives: {},
  data() {
    return {
        laber:{
            create_date:DateUtil.today(),
        },
        tenderPurchasePlan: {
            id: '',
            date: DateUtil.today(),
            planEditor: sessionStorage.getItem('realName')
        },
        fileList:[],
        projectId:'',//所属项目id
        providerId:'',//班组id
        empId:'',//签订人id

        summoney:'',//合计
        summoney2:'',//合计整数

        WJSC:false,//隐藏上传
        dialogVisible:false,//隐藏子组件
        dialogVisible2:false,//隐藏子组件
        dialogVisible3:false,//隐藏子组件

        tenderPurchaseDetails:[],
        Yes:false,
    }
  },
  mounted() {},
  methods: {

      //转换器
      toChies(values){//形参
					let len=values.length//统计出长度
					let arr=[];
					
					let chin_list=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']//所有的数值对应的汉字
					
					let chin_lisp=['仟','佰','拾','亿','仟','佰','拾','万','仟','佰','拾']//进制
					
					for(let i=0;i<len;i++){
						arr.push(parseInt(values[i]));		//输入的数据按下标存进去   存进去的只是数字
						arr[i]=chin_list[arr[i]]			//是根据我们输入的输入的数字，对应着我们的chin_list这个数组
					}//123['壹','佰','贰','拾','叁']
					
					for(let i=len-1,j=1;i>0;i--){//i =2	1		//倒序		为了添加进制，方便我们去观看
						arr.splice(i,0,chin_lisp[chin_lisp.length-j++])	//j=2
					}
					console.log(arr)
					
					arr=arr.join('')
					if(len>=1){
						arr+='元整'
					}
					return arr
				},

      //金额转换
      money(){
          var money = this.laber.contract_amount;
          var Maxmoney = this.toChies(money);
          console.log(Maxmoney)
          this.laber.amount_upper=Maxmoney
      },

      //开启隐藏项目子组件
      pid(){
           this.dialogVisible = true;
      },

      //响应项目组件
      project(row){
            this.laber.p_id = row.projectName;
            this.projectId = row.id;//获取所属项目id
            this.dialogVisible = false;
      },

      //开启隐藏班组子组件
      labororgid(){
          this.dialogVisible2 = true;
      },
      //响应班组子组件
      provider(row){
            this.laber.labor_org_id = row.provider_name;
            this.providerId = row.id;//获取所属项目id
            this.dialogVisible2 = false;
      },
      //开启隐藏员工子组件
      contractsignatory(){
          this.dialogVisible3 = true;
      },
      //响应员工子组件
      empDeptPost(row){
          this.laber.contract_signatory = row.name;
          this.empId = row.id;
           this.dialogVisible3 = false;
      },

      //添加一行
      handleAddRow() {
                this.tenderPurchaseDetails.push({
                    index: '',
                    work_content: '',
                    unit: '',
                    work_count: '',
                    unit_price: '',
                })
            },
        
        //删行
        handleRemoveRow(row) {
            this.tenderPurchaseDetails.splice(row.index, 1)
         },


         //文件上传
        handleUploadSuccess(ids,fileList){
            console.log(fileList)
                this.tenderPurchasePlan.attachemeIds = ids
            },



     

       //添加劳务合同
      Addlaber(){
          console.log(this.laber)
            //判断合同信息不能为空
          if(this.laber.create_date == '' || this.laber.create_date == undefined){
                this.$message.error('错误信息,日期不能为空');
          }else{
              if(this.laber.contract_name == '' || this.laber.contract_name == undefined){
                this.$message.error('错误信息,合同名称不能为空');
          }else{
              if(this.laber.p_id == '' || this.laber.p_id == undefined){
                this.$message.error('错误信息,所属项目不能为空');
          }else{
              if(this.laber.contract_type == '' || this.laber.contract_type == undefined){
                this.$message.error('错误信息,合同类型不能为空');
          }else{
              if(this.laber.contract_amount == '' || this.laber.contract_amount == undefined){
                this.$message.error('错误信息,合同金额不能为空');
          }else{
              if(this.laber.start_date == '' || this.laber.start_date == undefined){
                this.$message.error('错误信息,开始日期不能为空');
          }else{
              if(this.laber.end_date == '' || this.laber.end_date == undefined){
                this.$message.error('错误信息,结束日期不能为空');
          }else{
              if(this.laber.labor_org_id == '' || this.laber.labor_org_id == undefined){
                this.$message.error('错误信息,劳务班组不能为空');
          }else{
              if(this.laber.settle_type == '' || this.laber.settle_type == undefined){
                this.$message.error('错误信息,结算方式不能为空');
          }else{
              if(this.laber.advance_pay == '' || this.laber.advance_pay == undefined){
                this.$message.error('错误信息,预付款不能为空');
          }else{
              if(this.laber.deposit_amount == '' || this.laber.deposit_amount == undefined){
                this.$message.error('错误信息,保证金不能为空');
          }else{
              if(this.laber.payment_type == '' || this.laber.payment_type == undefined){
                this.$message.error('错误信息,付款方式不能为空');
          }else{
              if(this.laber.contract_signatory == '' || this.laber.contract_signatory == undefined){
                this.$message.error('错误信息,签订人不能为空');
          }else{
              if(this.laber.pay_condition == '' || this.laber.pay_condition == undefined){
                this.$message.error('错误信息,付款条件不能为空');
          }else{
              if(this.laber.main_term == '' || this.laber.main_term == undefined){
                this.$message.error('错误信息,主要条款不能为空');
          }else{
              if(this.laber.comment == '' || this.laber.comment == undefined){
                this.$message.error('错误信息,备注不能为空');
          }else{
              //判断工程量清单
                console.log(this.tenderPurchaseDetails)
                let index2 =0;
                for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                    if(this.tenderPurchaseDetails[index].work_content == ''){
                        this.$message.error('错误信息,工作内容不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].unit == ''){
                        this.$message.error('错误信息,单位不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].work_count == ''){
                        this.$message.error('错误信息,工程量不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].unit_price == ''){
                        this.$message.error('错误信息,单价不能为空');
                }else{
                    index2++;
                }
                }
                }
                }
                }

                if(index2 == this.tenderPurchaseDetails.length ){
                    this.laber.attach_ids=this.tenderPurchasePlan.attachemeIds;
                    console.log(this.laber.attach_ids)
                          Laowuhetong.Addlaber(this.laber,this.projectId,this.providerId,this.empId).then((response)=>{
                              this.$message({ message: '添加成功',  type: 'success' });
                              this.laber.id = response.data;
                              this.Yes=true;
                              if(this.tenderPurchaseDetails.length>0){
                                    for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                                            Laowuhetong.AddWorkList(this.tenderPurchaseDetails[index],this.laber.id).then(()=>{}) 
                                    }
                                  }
                                
                              

                             if(this.laber.attach_ids != '' || this.laber.attach_ids != undefined){
                                  Laowuhetong.UpdateInfo(this.laber.attach_ids,sessionStorage.getItem('realName')).then(()=>{})
                              }
                          })
                }
              }
            }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          }
          
          

      },

      //提交审批
      examine(){
            Laowuhetong.FlowLaber(this.laber).then((response)=>{
                if(response.data == 1){
                    this.$message({ message: '提交成功',  type: 'success' });
                     this.Yes=false;
                }else{
                    this.$message.error('提交失败');
                }
                
            }) 
      },
  
  },
  computed: {
    token: {
      get() {
        return this.$store.state.token;
      }
    },
    checkRoute() {
      return Object.keys(this.$route.params).length != 0;
    }
  },
  created() {}
};
</script>

<style>
.divtable {
  border-collapse: collapse;
}
.divtable td {
  padding: 10px;
  border: 1px solid rgb(7, 136, 175);
}
.tabletd {
  text-align: right;
  background-color: rgb(113, 222, 241);
}
.color {
  background-color: rgb(113, 222, 241);
}
.divtable2 {
  border-collapse: collapse;
}
.divtable2 td {
  padding: 10px;
  border: 1px solid rgb(7, 136, 175);
}
.td {
  text-align: center;
}
</style>